//index.js
//获取应用实例
var app = getApp();

Page({
  data: {
    totalScoreToPay: 0,
    goodsList:[],
    isNeedLogistics:1, // 是否需要物流信息
    allGoodsPrice:0,
    yunPrice:0,
    allGoodsAndYunPrice:0,
    goodsJsonStr:"",
    inquirySheetId:0,
    curAddressData:'',

    hasNoCoupons: true,
  },
  onShow : function () {
    var that = this;
    var shopList = [];
    // //立即购买下单
    // if ("buyNow"==that.data.orderType){
    //   var buyNowInfoMem = wx.getStorageSync('buyNowInfo');
    //   that.data.kjId = buyNowInfoMem.kjId;
    //   if (buyNowInfoMem && buyNowInfoMem.shopList) {
    //     shopList = buyNowInfoMem.shopList
    //   }
    // }else{
    //   //购物车下单
    //   var shopCarInfoMem = wx.getStorageSync('shopCarInfo');
    //   that.data.kjId = shopCarInfoMem.kjId;
    //   if (shopCarInfoMem && shopCarInfoMem.shopList) {
    //     // shopList = shopCarInfoMem.shopList
    //     shopList = shopCarInfoMem.shopList.filter(entity => {
    //       return entity.active;
    //     });
    //   }
    // }
    wx.request({
      url: app.globalData._path + app.globalData.subDomain + '/inquiry/detail',
      data:{
        id:this.data.inquirySheetId
      },
      success:function(res){
        if(res.data.code == 0){
          that.setData({
            goodsList: res.data.list,
          });
        }else{
          wx.showModal({
            title: '错误',
            content: res.data.list,
            showCancel: false,
            complete:function(){
              wx.navigateBack();
            }
          });
        }
      }
    })
    
    that.initShippingAddress();
  },

  onLoad: function (e) {
    this.setData({
      inquirySheetId:e.id
    });
  },

  getDistrictId : function (obj, aaa){
    if (!obj) {
      return "";
    }
    if (!aaa) {
      return "";
    }
    return aaa;
  },

  createOrder:function (e) {
    wx.showLoading();
    var that = this;
    var loginToken = wx.getStorageSync('token') // 用户登录 token
    var remark = ""; // 备注信息
    if (e) {
      remark = e.detail.value.remark; // 备注信息
    }
    
    if (!that.data.curAddressData) {
      wx.hideLoading();
      wx.showModal({
        title: '错误',
        content: '请先设置您的收货地址！',
        showCancel: false
      })
      return;
    }  
  
    wx.request({
      url: app.globalData._path+ app.globalData.subDomain +'/order',
      method:'POST',
      data: {
        uid: wx.getStorageSync('uid'),
        inquiry_sheet_id: that.data.inquirySheetId,
        address_id: that.data.curAddressData.id,
        remark: remark
      }, // 设置请求的 参数
      success: (res) =>{
        wx.hideLoading();
        if (res.data.code != 0) {
          wx.showModal({
            title: '错误',
            content: res.data.list,
            showCancel: false
          })
          return;
        }
        var dataList = res.data.list[0];

        // 配置模板消息推送
        var postJsonString = {};
        postJsonString.keyword1 = { value: dataList.order.order_id, color: '#173177' }
        postJsonString.keyword2 = { value: dataList.inquiry.goods_name, color: '#173177' }
        postJsonString.keyword3 = { value: dataList.inquiry.goods_name + ",规格:" + dataList.inquiry.goods_spec, color: '#173177' }
        postJsonString.keyword4 = { value: dataList.order.price + '元', color: '#173177' }
        postJsonString.keyword5 = { value: dataList.order.created_at, color: '#173177' }
        postJsonString.keyword6 = { value: '请尽快完成订单支付', color: '#173177' }
        app.sendTempleMsg(dataList.order.order_id, -1,
          'h1rz9gSzcjP8TJfrgJYdgLfM37dX6aLJ2NmMqusxaTY', e.detail.formId,
          'pages/order-list/index', JSON.stringify(postJsonString));
        // 下单成功，跳转到订单管理界面
        wx.redirectTo({
          url: "/pages/order-list/index"
        });
      }
    })
  },
  initShippingAddress: function () {
    var that = this;
    wx.request({
      url: app.globalData._path + app.globalData.subDomain +'/user/default.address',
      data: {
        uid: wx.getStorageSync('uid')
      },
      success: (res) =>{
        if (res.data.code == 0) {
          that.setData({
            curAddressData:res.data.list
          });
        }else{
          that.setData({
            curAddressData: null
          });
        }
        //that.processYunfei();
      }
    })
  },
  addAddress: function () {
    wx.navigateTo({
      url:"/pages/address-add/index"
    })
  },
  selectAddress: function () {
    wx.navigateTo({
      url:"/pages/select-address/index"
    })
  }
})
